# -*- coding: utf-8 -*-
'''
@File  : war-deploy.py
@Author: canx
@Date  : 2018/8/10 16:37
@Desc  :
        war包部署
'''

import os
import sshForLinux
import sys
import configparser



os.chdir(sys.path[0])
cf = configparser.ConfigParser()
cf.read("configfile/oto-product-deploy.ini")


# test or product
mvnProfile = cf.get('winConfig', 'mvnProfile')
projectWinPath = cf.get('winConfig', 'projectWinPath')
javaMvnCmd = cf.get('winConfig', 'javaMvnCmd')

projectLinuxPath = cf.get('linuxConfig', 'projectLinuxPath')

local = cf.get('winConfig', 'local')
remote = cf.get('linuxConfig', 'remote')

currentProfileCmd = cf.get('linuxConfig', 'currentProfileCmd')
stopAndBackUpCmd = cf.get('linuxConfig', 'stopAndBackUpCmd').split(",")
startCmd = cf.get('linuxConfig', 'startCmd').split(",")

ip = cf.get('host', 'ip')
username = cf.get('host', 'username')
passwd = cf.get('host', 'passwd')


# step 1 java project package
os.chdir(projectWinPath)
print("step 1 project package")
# system 成功返回0，否则返回1

os.system("git checkout .")
os.system("git pull")
result = os.system(javaMvnCmd)
print(result)

# step 2 stop jar service stop && backup jar
#你要执行的命令列表
print("step 2 backup war and remove project dir")
sshForLinux.ssh2_cmd(ip, username, passwd, stopAndBackUpCmd)

# step 3 sftp the jar to linux
print("step 3 file put")
sshForLinux.sftp_upload(ip, username, passwd, local, remote)

'''
# step 4  jar service start
print("step 4 service start")
print(startCmd)
sshForLinux.ssh2_cmd(ip, username, passwd, startCmd)
'''